package banmuoi.servlet;

import java.io.IOException;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import banmuoi.entity.Customer;
import banmuoi.entity.Order;
import banmuoi.util.HibernateUtil;

@WebServlet("/crudOrderCustomer.do")
public class OrderCustom extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		if (req.getParameter("userid") != null)
		{
			Session session = HibernateUtil.openSession();
			String userid = req.getParameter("userid");
			
			req.setAttribute("userId", userid);
			
			
			String hql = "FROM Order o where o.customer.id = " + userid;
			Query query = session.createQuery(hql);
			List<Order> entities = query.list();
			req.setAttribute("entityOrder", entities);
			
			if (req.getParameter("orderid") != null && req.getParameter("orderid") != "")
			{
				Order entity = (Order) session.load(Order.class, Integer.parseInt(req.getParameter("orderid")));
				req.setAttribute("entityOrderCustom", entity);
			} 
			
			session.close();
		}
		else
		{
			resp.sendRedirect("crudCustomers.do");
			return;
		}
		
		
		req.getRequestDispatcher("OrderCustomer.jsp").forward(req, resp);			
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String id = req.getParameter("txtId");
		String note = req.getParameter("txtNote");
		String address = req.getParameter("txtAddress");
		int userId = Integer.parseInt(req.getParameter("txtUserId"));
		
		Session session = HibernateUtil.openSession();
		Transaction trans = session.beginTransaction();
		
		try{
			if (req.getParameter("btnCreate") != null)
			{
				Order or = new Order();
				Customer cus = new Customer();
				cus.setId(userId);
				
				java.sql.Date ngay=new java.sql.Date(new java.util.Date().getTime());
				
				or.setTotal(Float.parseFloat("0"));
				or.setState(false);
				or.setAddress(address);
				or.setNote(note);
				or.setCustomer(cus);
				
				or.setOrderDate(ngay);
				
				session.save(or);
				
				req.setAttribute("error", "Insert Success!");
			}
			else if (req.getParameter("btnUpdate") != null)
			{
				Order entity = (Order) session.load(Order.class, Integer.parseInt(id));
				
				entity.setNote(note);
				entity.setAddress(address);
				
				session.update(entity);
				req.setAttribute("error", "Update Success!");
			} else
				if (req.getParameter("btnDelete") != null)
				{
					Order or = new Order();
					
					or.setId(Integer.parseInt(id));
					
					session.delete(or);
					req.setAttribute("error", "Delete Success!");
				}
			
			trans.commit();
		} catch (Exception ex)
		{
			trans.rollback();
			req.setAttribute("error", ex.getMessage());
		}
		
		session.close();
		doGet(req, resp);
	}
	
	
	
}
